﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using VideoRentalShop.Controller;
using VideoRentalShop.View.Templates;
using VideoRentalShop.Model;

namespace VideoRentalShop.View.Panel.ReturnStepPanel
{
    /// <summary>
    /// Logika interakcji dla klasy RentSummaryStepPanel.xaml
    /// </summary>
    public partial class ReturnSummaryStepPanel : StepPanelContent
    {
        private VideoRentalShopController _controller = VideoRentalShopController.Instance;


        public ReturnSummaryStepPanel()
        {
            InitializeComponent();
        }


        public override void RefreshForm()
        {
            var item = _item as OrderReturn;

            if (item == null)
            {
                ClearForm();
                return;
            }

            var clientId = item.ClientId;
            var movies = item.Movies;
            var date = item.Date = DateTime.Now;
            lblDate.Content = date.ToString(VideoRentalShop.Properties.Resources.StrDateFormat);

            var client = _controller.Data.Client.Get(clientId);
            if (client != null)
            {
                lblClient.Content = string.Format(string.Format("{0} {1}", client.LastName, client.Name));
            }

            lstMovies.Items.Clear();
            foreach (var movie in movies.Where(x => x.Returned))
            {
                var id = movie.MovieId;
                var overheld = "<brak>";

                if (movie.OverHeld && date > movie.MaxReturnDate)
                {
                    var overholding = date.Subtract(movie.MaxReturnDate);
                    overheld = string.Format("{0:D2}:{1:D2}", overholding.Hours, overholding.Minutes);
                }

                var m = _controller.Data.Movie.GetAll().Find(id);

                if (m != null)
                {
                    lstMovies.Items.Add(new
                    {
                        Title = m.Title,
                        Date = movie.Date,
                        Overheld = overheld
                    });
                }
            }
        }

        public override void ClearForm()
        {
            lblClient.Content = string.Empty;
            lblDate.Content = string.Empty;
            lstMovies.Items.Clear();
        }

        public override bool ValidateForm()
        {
            return AddReturn(_item as OrderReturn);
        }

        private bool AddReturn(OrderReturn itemData)
        {
            int complete = 0;
            int errors = 0;
            var date = itemData.Date;
            foreach (var item in itemData.Movies.Where(x => x.Returned))
            {
                var details = _controller.Data.OrderDetails
                    .GetAll()
                    .Where(x => x.MovieId == item.MovieId && x.OrderId == item.OrderId)
                    .FirstOrDefault();

                if (details != null)
                {
                    details.ReturnDate = date;
                    details.Movie.Avaiable++;

                    var archived = true;
                    foreach (var x in details.Order.OrderDetails)
                    {
                        archived &= x.ReturnDate != null;
                        if (!archived)
                        {
                            break;
                        }
                    }
                    if (archived)
                    {
                        details.Order.Archived = true;
                    }

                    var completed = _controller.Data.OrderDetails.Edit(details);

                    if (completed)
                    {
                        complete++;
                    }
                    else
                    {
                        errors++;
                    }
                }
                else
                {
                    errors++;
                }
            }

            if (errors == 0)
            {
                return true;
            }
            else if (complete > 0)
            {
                _controller.ShowMessage("Podczas wprowadzania niektórych zmian wystąpiły błędy.", "Błąd", MessageType.Error);
            }
            else
            {
                _controller.ShowMessage("Podczas wprowadzania zmian wystąpiły błędy.", "Błąd", MessageType.Error);
            }

            return false;
        }
    }
}
